import React, { Component } from 'react';
import {
    View,
    Text,
    StyleSheet,
    TouchableOpacity,
} from 'react-native';

import { connect } from 'react-redux'; // 引入connect函数
import *as loginAction from '../actions/LoginAction';// 导入action方法
import { StackActions,NavigationActions } from 'react-navigation';
import GetSetStorge from "../utils/GetSetStorge";


const resetAction = StackActions.reset({
    index: 0,
    actions: [
        NavigationActions.navigate({routeName:'Main'})//要跳转到的页面名字
    ]
});
class LoginPage extends Component {
    componentDidMount() {
        GetSetStorge.getStorgeAsync('isLogin').then((result) => {
            if (result != null && result != ''&&result=='true') {
                this.props.navigation.dispatch(resetAction);
            }
        }).catch((error) => {
            console.log('==========================');
            console.log('系统异常' + error);
            console.log('==========================');
        });
    }
    shouldComponentUpdate(nextProps, nextState) {
        // 登录完成,切成功登录
        if (nextProps.status === '登陆成功' && nextProps.isSuccess) {
            this.props.navigation.dispatch(resetAction);
            return false;
        }
        return true;
    }

    render() {
        const { login } = this.props;
        return(
            <View style={styles.container}>
                <Text>状态: {this.props.status}
                </Text>
                <TouchableOpacity onPress={()=>login()} style={{marginTop: 50}}>
                    <View style={styles.loginBtn}>
                        <Text>登录
                        </Text>
                    </View>
                </TouchableOpacity>
            </View>
        )
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center',
        backgroundColor: '#F5FCFF'
    },
    loginBtn: {
        borderWidth: 1,
        padding: 5,
    }
});

export default connect(
    (state) => ({
        status: state.loginIn.status,
        isSuccess: state.loginIn.isSuccess,
        user: state.loginIn.user,
    }),
    (dispatch) => ({
        login: () => dispatch(loginAction.login()),
    })
)(LoginPage)